package com.example.namibox_book_sdk;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.TimeUnit;
import namibox.booksdk.NamiboxSDKManager;

//模拟订购
public class OrderActivity extends AppCompatActivity {
  String bookid;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_order);
    TextView textView1 = findViewById(R.id.text1);
    TextView textView2 = findViewById(R.id.text2);
    Intent intent = getIntent();
    String type = intent.getStringExtra("type");
    bookid = intent.getStringExtra("bookid");
    textView1.setText("bookid:" + bookid);
    textView2.setText("type:" + type);
    findViewById(R.id.btn).setOnClickListener(v -> {
      refreshBookOrder();
      finish();
    });
    findViewById(R.id.btn1).setOnClickListener(v -> {
      refreshOralOrder();
      finish();
    });
    findViewById(R.id.btn2).setOnClickListener(v -> finish());
  }

  private void refreshBookOrder() {
    Observable.timer(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Long>() {
          @Override
          public void onSubscribe(@NonNull Disposable d) {

          }

          @Override
          public void onNext(@NonNull Long aLong) {
            NamiboxSDKManager.getInstance().refreshOrder(bookid);
          }

          @Override
          public void onError(@NonNull Throwable e) {

          }

          @Override
          public void onComplete() {

          }
        });
  }

  private void refreshOralOrder() {
    Observable.timer(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Long>() {
          @Override
          public void onSubscribe(@NonNull Disposable d) {

          }

          @Override
          public void onNext(@NonNull Long aLong) {
            NamiboxSDKManager.getInstance().refreshOral();
          }

          @Override
          public void onError(@NonNull Throwable e) {

          }

          @Override
          public void onComplete() {

          }
        });
  }

}